home *** CD-ROM | disk | FTP | other *** search
/ LG Super CD / LG Super CD.iso / bitpim / bitpim-0.62-setup.exe / {app} / bitpim.exe / cmd.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-11-06  |  9.5 KB  |  336 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.3)
  3.  
  4. import string
  5. __all__ = [
  6.     'Cmd']
  7. PROMPT = '(Cmd) '
  8. IDENTCHARS = string.ascii_letters + string.digits + '_'
  9.  
  10. class Cmd:
  11.     prompt = PROMPT
  12.     identchars = IDENTCHARS
  13.     ruler = '='
  14.     lastcmd = ''
  15.     intro = None
  16.     doc_leader = ''
  17.     doc_header = 'Documented commands (type help <topic>):'
  18.     misc_header = 'Miscellaneous help topics:'
  19.     undoc_header = 'Undocumented commands:'
  20.     nohelp = '*** No help on %s'
  21.     use_rawinput = 1
  22.     
  23.     def __init__(self, completekey = 'tab', stdin = None, stdout = None):
  24.         import sys
  25.         if stdin is not None:
  26.             self.stdin = stdin
  27.         else:
  28.             self.stdin = sys.stdin
  29.         if stdout is not None:
  30.             self.stdout = stdout
  31.         else:
  32.             self.stdout = sys.stdout
  33.         self.cmdqueue = []
  34.         self.completekey = completekey
  35.  
  36.     
  37.     def cmdloop(self, intro = None):
  38.         self.preloop()
  39.         if intro is not None:
  40.             self.intro = intro
  41.         
  42.         if self.intro:
  43.             self.stdout.write(str(self.intro) + '\n')
  44.         
  45.         stop = None
  46.         while not stop:
  47.             if self.cmdqueue:
  48.                 line = self.cmdqueue.pop(0)
  49.             elif self.use_rawinput:
  50.                 
  51.                 try:
  52.                     line = raw_input(self.prompt)
  53.                 except EOFError:
  54.                     line = 'EOF'
  55.                 except:
  56.                     None<EXCEPTION MATCH>EOFError
  57.                 
  58.  
  59.             None<EXCEPTION MATCH>EOFError
  60.             self.stdout.write(self.prompt)
  61.             self.stdout.flush()
  62.             line = self.stdin.readline()
  63.             if not len(line):
  64.                 line = 'EOF'
  65.             else:
  66.                 line = line[:-1]
  67.             line = self.precmd(line)
  68.             stop = self.onecmd(line)
  69.             stop = self.postcmd(stop, line)
  70.         self.postloop()
  71.  
  72.     
  73.     def precmd(self, line):
  74.         return line
  75.  
  76.     
  77.     def postcmd(self, stop, line):
  78.         return stop
  79.  
  80.     
  81.     def preloop(self):
  82.         if self.completekey:
  83.             
  84.             try:
  85.                 import readline
  86.                 self.old_completer = readline.get_completer()
  87.                 readline.set_completer(self.complete)
  88.                 readline.parse_and_bind(self.completekey + ': complete')
  89.             except ImportError:
  90.                 pass
  91.             except:
  92.                 None<EXCEPTION MATCH>ImportError
  93.             
  94.  
  95.         None<EXCEPTION MATCH>ImportError
  96.  
  97.     
  98.     def postloop(self):
  99.         if self.completekey:
  100.             
  101.             try:
  102.                 import readline
  103.                 readline.set_completer(self.old_completer)
  104.             except ImportError:
  105.                 pass
  106.             except:
  107.                 None<EXCEPTION MATCH>ImportError
  108.             
  109.  
  110.         None<EXCEPTION MATCH>ImportError
  111.  
  112.     
  113.     def parseline(self, line):
  114.         line = line.strip()
  115.         if not line:
  116.             return (None, None, line)
  117.         elif line[0] == '?':
  118.             line = 'help ' + line[1:]
  119.         elif line[0] == '!':
  120.             if hasattr(self, 'do_shell'):
  121.                 line = 'shell ' + line[1:]
  122.             else:
  123.                 return (None, None, line)
  124.         
  125.         (i, n) = (0, len(line))
  126.         while i < n and line[i] in self.identchars:
  127.             i = i + 1
  128.         (cmd, arg) = (line[:i], line[i:].strip())
  129.         return (cmd, arg, line)
  130.  
  131.     
  132.     def onecmd(self, line):
  133.         (cmd, arg, line) = self.parseline(line)
  134.         if not line:
  135.             return self.emptyline()
  136.         
  137.         if cmd is None:
  138.             return self.default(line)
  139.         
  140.         self.lastcmd = line
  141.         if cmd == '':
  142.             return self.default(line)
  143.         else:
  144.             
  145.             try:
  146.                 func = getattr(self, 'do_' + cmd)
  147.             except AttributeError:
  148.                 return self.default(line)
  149.  
  150.             return func(arg)
  151.  
  152.     
  153.     def emptyline(self):
  154.         if self.lastcmd:
  155.             return self.onecmd(self.lastcmd)
  156.         
  157.  
  158.     
  159.     def default(self, line):
  160.         self.stdout.write('*** Unknown syntax: %s\n' % line)
  161.  
  162.     
  163.     def completedefault(self, *ignored):
  164.         return []
  165.  
  166.     
  167.     def completenames(self, text, *ignored):
  168.         dotext = 'do_' + text
  169.         return []
  170.  
  171.     
  172.     def complete(self, text, state):
  173.         if state == 0:
  174.             import readline
  175.             origline = readline.get_line_buffer()
  176.             line = origline.lstrip()
  177.             stripped = len(origline) - len(line)
  178.             begidx = readline.get_begidx() - stripped
  179.             endidx = readline.get_endidx() - stripped
  180.             if begidx > 0:
  181.                 (cmd, args, foo) = self.parseline(line)
  182.             None if cmd == '' else None<EXCEPTION MATCH>AttributeError
  183.             compfunc = self.completenames
  184.             self.completion_matches = compfunc(text, line, begidx, endidx)
  185.         
  186.         
  187.         try:
  188.             return self.completion_matches[state]
  189.         except IndexError:
  190.             return None
  191.  
  192.  
  193.     
  194.     def get_names(self):
  195.         names = []
  196.         classes = [
  197.             self.__class__]
  198.         while classes:
  199.             aclass = classes.pop(0)
  200.             if aclass.__bases__:
  201.                 classes = classes + list(aclass.__bases__)
  202.             
  203.             names = names + dir(aclass)
  204.         return names
  205.  
  206.     
  207.     def complete_help(self, *args):
  208.         return self.completenames(*args)
  209.  
  210.     
  211.     def do_help(self, arg):
  212.         if arg:
  213.             
  214.             try:
  215.                 func = getattr(self, 'help_' + arg)
  216.             except AttributeError:
  217.                 
  218.                 try:
  219.                     doc = getattr(self, 'do_' + arg).__doc__
  220.                     if doc:
  221.                         self.stdout.write('%s\n' % str(doc))
  222.                         return None
  223.                 except AttributeError:
  224.                     pass
  225.  
  226.                 self.stdout.write('%s\n' % str(self.nohelp % (arg,)))
  227.                 return None
  228.  
  229.             func()
  230.         else:
  231.             names = self.get_names()
  232.             cmds_doc = []
  233.             cmds_undoc = []
  234.             help = { }
  235.             for name in names:
  236.                 if name[:5] == 'help_':
  237.                     help[name[5:]] = 1
  238.                     continue
  239.             
  240.             names.sort()
  241.             prevname = ''
  242.             for name in names:
  243.                 if name[:3] == 'do_':
  244.                     if name == prevname:
  245.                         continue
  246.                     
  247.                     prevname = name
  248.                     cmd = name[3:]
  249.                     if cmd in help:
  250.                         cmds_doc.append(cmd)
  251.                         del help[cmd]
  252.                     elif getattr(self, name).__doc__:
  253.                         cmds_doc.append(cmd)
  254.                     else:
  255.                         cmds_undoc.append(cmd)
  256.                 cmd in help
  257.             
  258.             self.stdout.write('%s\n' % str(self.doc_leader))
  259.             self.print_topics(self.doc_header, cmds_doc, 15, 80)
  260.             self.print_topics(self.misc_header, help.keys(), 15, 80)
  261.             self.print_topics(self.undoc_header, cmds_undoc, 15, 80)
  262.  
  263.     
  264.     def print_topics(self, header, cmds, cmdlen, maxcol):
  265.         if cmds:
  266.             self.stdout.write('%s\n' % str(header))
  267.             if self.ruler:
  268.                 self.stdout.write('%s\n' % str(self.ruler * len(header)))
  269.             
  270.             self.columnize(cmds, maxcol - 1)
  271.             self.stdout.write('\n')
  272.         
  273.  
  274.     
  275.     def columnize(self, list, displaywidth = 80):
  276.         if not list:
  277.             self.stdout.write('<empty>\n')
  278.             return None
  279.         
  280.         nonstrings = []
  281.         if nonstrings:
  282.             raise TypeError, 'list[i] not a string for i in %s' % ', '.join(map(str, nonstrings))
  283.         
  284.         size = len(list)
  285.         if size == 1:
  286.             self.stdout.write('%s\n' % str(list[0]))
  287.             return None
  288.         
  289.         for nrows in range(1, len(list)):
  290.             ncols = (size + nrows - 1) // nrows
  291.             colwidths = []
  292.             totwidth = -2
  293.             for col in range(ncols):
  294.                 colwidth = 0
  295.                 for row in range(nrows):
  296.                     i = row + nrows * col
  297.                     if i >= size:
  298.                         break
  299.                     
  300.                     x = list[i]
  301.                     colwidth = max(colwidth, len(x))
  302.                 
  303.                 colwidths.append(colwidth)
  304.                 totwidth += colwidth + 2
  305.                 if totwidth > displaywidth:
  306.                     break
  307.                     continue
  308.             
  309.             if totwidth <= displaywidth:
  310.                 break
  311.                 continue
  312.         else:
  313.             nrows = len(list)
  314.             ncols = 1
  315.             colwidths = [
  316.                 0]
  317.         for row in range(nrows):
  318.             texts = []
  319.             for col in range(ncols):
  320.                 i = row + nrows * col
  321.                 if i >= size:
  322.                     x = ''
  323.                 else:
  324.                     x = list[i]
  325.                 texts.append(x)
  326.             
  327.             while texts and not texts[-1]:
  328.                 del texts[-1]
  329.             for col in range(len(texts)):
  330.                 texts[col] = texts[col].ljust(colwidths[col])
  331.             
  332.             self.stdout.write('%s\n' % str('  '.join(texts)))
  333.         
  334.  
  335.  
  336.